#include <stdio.h>
#include "language.h"

char* G_HOME_PAGE_NAME = NULL;;
char* G_LIST_PAGE_NAME = NULL;;
char* G_SEARCH_PAGE_NAME = NULL;;

char* G_HOME = NULL;
char* G_LIST = NULL;
char* G_SEARCH = NULL;
char* G_LOGIN = NULL;
char* G_ALL = NULL;
char* G_NAME = NULL;
char* G_ADDRESS = NULL;
char* G_FATHER = NULL;
char* G_MOTHER = NULL;
char* G_WIFE = NULL;
char* G_CHILDREN = NULL;
char* G_DISCRIPTION = NULL;
char* G_SORRY = NULL;
char* G_ERROR = NULL;

char* G_TITLE_INFO = NULL;
char* G_ERR_INFO = NULL;
char* G_COPYRIGHT_INFO = NULL;


static char* M_HOME[LANG_NUM];
static char* M_LIST[LANG_NUM];
static char* M_SEARCH[LANG_NUM];
static char* M_LOGIN[LANG_NUM];
static char* M_ALL[LANG_NUM];
static char* M_NAME[LANG_NUM];
static char* M_ADDRESS[LANG_NUM];
static char* M_FATHER[LANG_NUM];
static char* M_MOTHER[LANG_NUM];
static char* M_NAME[LANG_NUM];
static char* M_WIFE[LANG_NUM];
static char* M_CHILDREN[LANG_NUM];
static char* M_DISCRIPTION[LANG_NUM];
static char* M_SORRY[LANG_NUM];
static char* M_ERROR[LANG_NUM];

static char* M_TITLE_INFO[LANG_NUM];
static char* M_ERR_INFO[LANG_NUM];
static char* M_COPYRIGHT_INFO[LANG_NUM];

int is_init_language = 0;
int
check_language()
{
	int ret;
	char *args, hl[10], hl_arg[10];

	args = getenv("QUERY_STRING");
	ret = getval(hl, "hl", args);

	if (is_init_language == 0) {
		init_language();
		is_init_language = 1;
	}
	
	if (ret == 1) {
		if (strcmp(hl, "en") == 0) {
			set_language(ENGLISH);
		}else if (strcmp(hl, "ja") == 0) {
			set_language(JAPANESE);
		}else {
			set_language(CHINESE);
		}
		sprintf(hl_arg, "hl=%s", hl);

		char tmp[128];
		sprintf(tmp, "home?hl=%s", hl);
		G_HOME_PAGE_NAME = strdup(tmp);
		sprintf(tmp, "list?hl=%s", hl);
		G_LIST_PAGE_NAME = strdup(tmp);
		sprintf(tmp, "search?hl=%s", hl);
		G_SEARCH_PAGE_NAME = strdup(tmp);
	}else {
		//set_language(CHINESE);
		set_language(DEFAULT_LANGUAGE);
		G_HOME_PAGE_NAME = strdup("home?hl=en");
		G_LIST_PAGE_NAME = strdup("list?hl=en");
		G_SEARCH_PAGE_NAME = strdup("search?hl=en");
	}

	return 0;
}

int
convert_language(char *v, char *s, char *d)
{
	char *p, tmp[128];
	char* n = "hl";
	int i = 0, flag = 0;
	if ((p = strstr(s, n)) != 0) {
		flag = 1;
		while (*p != '=') {
			p++;
		}
		++p;
		i = p - s;
		strncpy(tmp, s, i);
		tmp[i] = '\0';
		strcat(tmp, v);
		i += strlen(v);
		tmp[i] = '\0';
		while (*p != '&' && *p != '\0') {
			p++;
		}
		strcat(tmp, p);
		i += strlen(p);
		tmp[i] = '\0';
		strcpy(d, tmp);
	}else {
		strcat(tmp, s);
		sprintf(tmp, "%s?hl=%s", s, v);
		strcpy(d, tmp);
	}
	return flag;
}

int set_language(int type)
{
	G_HOME				= M_HOME[type];
	G_LIST				= M_LIST[type];
	G_SEARCH			= M_SEARCH[type];
	G_LOGIN 			= M_LOGIN[type];
	G_ALL				= M_ALL[type];
	G_NAME				= M_NAME[type];
	G_ADDRESS			= M_ADDRESS[type];
	G_FATHER			= M_FATHER[type];
	G_MOTHER			= M_MOTHER[type];
	G_WIFE				= M_WIFE[type];
	G_CHILDREN			= M_CHILDREN[type];
	G_DISCRIPTION		= M_DISCRIPTION[type];
	G_SORRY 			= M_SORRY[type];
	G_ERROR 			= M_ERROR[type];
	G_TITLE_INFO		= M_TITLE_INFO[type];
	G_ERR_INFO			= M_ERR_INFO[type];
	G_COPYRIGHT_INFO	= M_COPYRIGHT_INFO[type];

	return 0;
}

int init_language()
{

	M_HOME[ENGLISH]				= strdup("Home");
	M_LIST[ENGLISH]				= strdup("List");
	M_SEARCH[ENGLISH]			= strdup("Search");
	M_LOGIN[ENGLISH]				= strdup("Login");
	M_ALL[ENGLISH]				= strdup("All");
	M_NAME[ENGLISH]				= strdup("Name");
	M_ADDRESS[ENGLISH]			= strdup("Address");
	M_FATHER[ENGLISH]			= strdup("Father");
	M_MOTHER[ENGLISH]			= strdup("Mother");
	M_NAME[ENGLISH]				= strdup("Name");
	M_WIFE[ENGLISH]				= strdup("Wife");
	M_CHILDREN[ENGLISH]			= strdup("Children");
	M_DISCRIPTION[ENGLISH]		= strdup("Discription");
	M_SORRY[ENGLISH]				= strdup("Sorry");
	M_ERROR[ENGLISH]				= strdup("Error");
	M_TITLE_INFO[ENGLISH]		= strdup("Simple Pedigree System");
	M_ERR_INFO[ENGLISH]			= strdup("Please contact me");
	M_COPYRIGHT_INFO[ENGLISH]	= strdup("@From 2008.02.13 wei");

	M_HOME[CHINESE]				= strdup("主页");                  
	M_LIST[CHINESE]				= strdup("列表");
	M_SEARCH[CHINESE]			= strdup("搜索");                
	M_LOGIN[CHINESE]				= strdup("登录");                 
	M_ALL[CHINESE]				= strdup("全部");                   
	M_NAME[CHINESE]				= strdup("姓名");                  
	M_ADDRESS[CHINESE]			= strdup("地址");               
	M_FATHER[CHINESE]			= strdup("父亲");                
	M_MOTHER[CHINESE]			= strdup("母亲");                
	M_WIFE[CHINESE]				= strdup("妻子");                  
	M_CHILDREN[CHINESE]			= strdup("孩子");              
	M_DISCRIPTION[CHINESE]		= strdup("描述");           
	M_SORRY[CHINESE]				= strdup("对不起");                 
	M_ERROR[CHINESE]				= strdup("错误");                 
	M_TITLE_INFO[CHINESE]		= strdup("简易家谱系统");
	M_ERR_INFO[CHINESE]			= strdup("出错啦");     
	M_COPYRIGHT_INFO[CHINESE]	= strdup("@从 2008.02.13 wei");  



	M_HOME[JAPANESE]				= strdup("#Home");                  
	M_LIST[JAPANESE]				= strdup("#List");                  
	M_SEARCH[JAPANESE]			= strdup("#Search");                
	M_LOGIN[JAPANESE]			= strdup("#Login");                 
	M_ALL[JAPANESE]				= strdup("#All");                   
	M_NAME[JAPANESE]				= strdup("#Name");                  
	M_ADDRESS[JAPANESE]			= strdup("#Address");               
	M_FATHER[JAPANESE]			= strdup("#Father");                
	M_MOTHER[JAPANESE]			= strdup("#Mother");                
	M_WIFE[JAPANESE]				= strdup("#Wife");                  
	M_CHILDREN[JAPANESE]			= strdup("#Children");              
	M_DISCRIPTION[JAPANESE]		= strdup("#Discription");           
	M_SORRY[JAPANESE]			= strdup("#Sorry");                 
	M_ERROR[JAPANESE]			= strdup("#Error");                 
	M_TITLE_INFO[JAPANESE]		= strdup("#Simple Pedigree System");
	M_ERR_INFO[JAPANESE]			= strdup("#Please contact me");     
	M_COPYRIGHT_INFO[JAPANESE]	= strdup("#@From 2008.02.13 wei");  

}

